package com.lzh.controller;


import com.github.pagehelper.PageInfo;
import com.lzh.pojo.*;
import com.lzh.service.*;
import com.lzh.service.impl.CEServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Reborn
 * @since 2020-06-15
 */
@Controller
@RequestMapping("/college")
public class CollegeController {

    @Autowired
    CollegeService collegeService;

    @Autowired
    CEService ceService;

    @Autowired
    ExamService examService;

    @Autowired
    StudentService studentService;

    @Autowired
    ScoreService scoreService;

    //查看该学院已有的考试
    @RequestMapping("/examByCo")
    public String examByCo(
            int id, Model model, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "15") Integer pageSize) {
        List<Integer> integers = ceService.queryEByC(id);
        PageInfo<Exam> examPageInfo = examService.queryEByCPage(integers, pageNum, pageSize);
        College college = collegeService.QueryCollegeOne(id);
        model.addAttribute("pageInfoL", examPageInfo);
        model.addAttribute("college", college);
        model.addAttribute("type", "have");
        List<College> colleges = collegeService.QueryCollege();
        model.addAttribute("colleges", colleges);
        return "college/exams";
    }

    //查看该学院未有的考试
    @RequestMapping("/examByCoNo")
    public String examByCoNo(int id, Model model,
                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                             @RequestParam(value = "pageSize", defaultValue = "15") Integer pageSize) {
        List<Integer> integers = ceService.queryEByC(id);
        Set<Integer> integers1 = ceService.queryEid();
        LinkedList<Integer> integers2 = new LinkedList<>();
        for (Integer integer : integers1) {
            int a = 0;
            for (Integer integer1 : integers) {
                if (integer == integer1) {
                    a = 1;
                    break;
                }
            }
            if (a == 0) {
                integers2.add(integer);
            }
        }
        PageInfo<Exam> examPageInfo = examService.queryEByCPage(integers2, pageNum, pageSize);
        College college = collegeService.QueryCollegeOne(id);
        model.addAttribute("pageInfoL", examPageInfo);
        model.addAttribute("college", college);
        model.addAttribute("type", "nohave");
        List<College> colleges = collegeService.QueryCollege();
        model.addAttribute("colleges", colleges);
        return "college/exams";
    }


    //添加学院与考试的联系
    @RequestMapping("/addCE")
    public String addCE(int ids[], int cid, Model model) {
        CE ce = new CE();
//        College college = collegeService.QueryCollegeOne(cid);
        LinkedList<Integer> integers = new LinkedList<>();
        List<Student> students = studentService.queryStudentByCid(cid);
        for (int id : ids) {
            ce.setCid(cid);
            ce.setEid(id);
            integers.add(id);
            ceService.insertCE(ce);
//            在给学院添加考试的同时也给学生添加考试成绩，默认为空
            Score score = new Score();
            score.setEid(id);
            for (Student student : students) {
                score.setSid(student.getId());
                scoreService.addScore(score);
            }
        }
        Integer pageNum = 1;
        Integer pageSize = 15;
        examByCo(cid, model, pageNum, pageSize);
        List<Exam> exams = examService.queryEByids(integers);
        model.addAttribute("exams", exams);
        model.addAttribute("type", "addok");
        return "college/exams";
    }


    //删除学院与考试的联系
    @RequestMapping("/deleteCE")
    public String deleteCE(int ids[], int cid, Model model) {
        CE ce = new CE();
        LinkedList<Integer> integers = new LinkedList<>();
        List<Student> students = studentService.queryStudentByCid(cid);
        for (int id : ids) {
            ce.setCid(cid);
            ce.setEid(id);
            integers.add(id);
            ceService.deleteCEByCE(ce);
            Score score = new Score();
            score.setEid(id);
            for (Student student : students) {
                score.setSid(student.getId());
                scoreService.deleteScore(score);
            }
        }
        Integer pageNum = 1;
        Integer pageSize = 15;
        examByCo(cid, model, pageNum, pageSize);
        List<Exam> exams = examService.queryEByids(integers);
        model.addAttribute("exams", exams);
        model.addAttribute("type", "deleteok");
        return "college/exams";
    }
}

